<template>
	<view class="settle-box">
		<view class="top">
			<radio :checked='chekChange' color="#C00000" style="transform:scale(0.9)" @click="checkClick"></radio>
			 <text>全选</text>
		</view>
		<view  class="main">
			<text>合计：</text>
			<text style="color:#C00000;font-weight:bold">￥{{totalPrice}}</text>
		</view>
		<view class="bottm" @click="GoLogin">
			<text>结算：</text>
			<text>({{totalNum}})</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checkVal:true,
				 total:0,
				 value:true
			};
		},
		computed:{
			totalPrice(){
				var c=0;
				uni.$Store.state.cartdata.cart.forEach(x=>{
					if(x.goods_state===true){
						c+=x.goods_price*x.goods_count
						this.totalNum+x.goods_count
					}
					
				})
				return c.toFixed(2)
			},
			totalNum(){
				var total=0
				uni.$Store.state.cartdata.cart.forEach(x=>{
					if(x.goods_state==true){
						total+=x.goods_count
					
					}
				},0)
			
				 return total
			},
			chekChange(){
				var s=0;
				uni.$Store.state.cartdata.cart.forEach(x=>{				
						s+=x.goods_price*x.goods_count
					
				})
				// s.toFixed(2)
				return s.toFixed(2)===this.totalPrice
			}
		},
		
		methods:{
			countDown(miao) {
				uni.showToast({
					icon:"none",
					title:"请先登录"+miao+"秒之后为您跳转登录页面",
					duration:1500,
					mask:true
				
				})
				
			},
			checkClick(){
				this.checkVal=!this.checkVal
			    uni.$Store.state.cartdata.cart.forEach(x=>{				
						x.goods_state=this.checkVal
				})
				uni.setStorageSync("cart",uni.$Store.state.cartdata.cart)
			},
			GoLogin(){
				var token=JSON.stringify(uni.$Store.state.cartdata.userInfo)
			    
				if(!this.totalNum) return uni.$showMsg("至少选择一件商品")
			    if(JSON.stringify(uni.$Store.state.cartdata.address)=="{}") return uni.$showMsg("请填写用户地址")
			    
				if(token=="{}"){
						var miao=3;
						if(this.value){
							this.value=!this.value
							var time=setInterval(()=>{
								this.countDown(miao)
								miao--
								if(miao<0){
									this.value=!this.value
									clearInterval(time)
									uni.switchTab({
										url:"/pages/My/My",
										success: () => {
											var redirect={
												openType:"switchTab",
												from:"/pages/cart/cart"
											}
										uni.$Store.commit("redirectInfo",redirect)
										uni.setStorageSync("skip",redirect)
										}
									})
								}
							},1000)
						}
					}else{
						uni.$showMsg("你还真敢结算")
					}
			},
			
		}
	}
</script>

<style lang="scss">
  .settle-box{
	  width: 100%;
	  height: 50px;
	  display: flex;
	  border:1px solid block;
		background-color: white;
		align-items: center;
		font-size:14px;
		justify-content: space-between;
		.top{
			display:flex;
			align-items: center;
		}
	  .bottm{
		  display:flex;
		  background-color: #C00000;
		  height:50px;
		  min-width: 100px;
		  align-items: center;
		  justify-content:center;
	  }
	  // .main{
		 // width:33%
	  // }
	  // .top{
		 //  width:33%;
		 //  padding-left:5px;
	  // }
  }
</style>
